package net.cityside.summer.web.security;

import java.util.ArrayList;
import java.util.Collection;

import net.cityside.summer.domain.Authority;
import net.cityside.summer.domain.User;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 这个类的成员 必须都是可序列化的
 * 
 * @author YaoYuan
 * 
 */
public class SecuredUser implements UserDetails {

	private static final long serialVersionUID = 1L;

	private final User user;
	private final Collection<SecuredAuthority> authorities;

	public SecuredUser(User user) {
		this.user = user;
		if (user.getAuthorities() == null) {
			throw new IllegalArgumentException("authorities==null");
		}

		authorities = new ArrayList<SecuredAuthority>(user.getAuthorities()
				.size());

		for (Authority authority : user.getAuthorities()) {
			authorities.add(new SecuredAuthority(authority));
		}
	}

	public User getUser() {
		return user;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return user.isEnabled();
	}

}
